package org.csu.mypetstore.controller;

import org.csu.mypetstore.common.CommonResponse;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.OrderService;
import org.csu.mypetstore.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/manage")
@SessionAttributes("allCategoryList")
public class ManageController {

    @Autowired
    private CatalogService catalogService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;


    @GetMapping("/viewOrderManage")
    public CommonResponse<Object> viewOrderManage(){

        List<Order> orderList = orderService.getAllOrder();
        System.out.println(orderList);
        return CommonResponse.createForSuccess(orderList);
    }
//    @GetMapping("/viewChangeOrder")
//    public String viewChangeOrder(int orderId,Model model){
//        Order order=orderService.getOrder(orderId);
//        System.out.println(order.getBillCity()+"viewChangeOrder");
//         //Order order=(Order) request.getSession().getAttribute("order");
//        model.addAttribute("order",order);
//        return "/manage/changeOrder";
//    }
    @PostMapping("/viewChangeOrder")
    public CommonResponse<Object> viewChangeOrder(@RequestParam("orderId") int orderId){
        Order order=orderService.getOrder(orderId);
        System.out.println(order.getBillCity()+"viewChangeOrder");
        //Order order=(Order) request.getSession().getAttribute("order");
        return CommonResponse.createForSuccess(order);
    }
    @GetMapping("/viewChangeCategory")
    public CommonResponse<Object> viewChangeCategory(@RequestParam("categoryId") String categoryId){
        Category category=catalogService.getCategory(categoryId);

        return CommonResponse.createForSuccess(category);
    }
    @GetMapping("/viewChangeProduct")
    public CommonResponse<Object> viewChangeProduct(@RequestParam("productId") String productId){
        Product product=catalogService.getProduct(productId);
        return CommonResponse.createForSuccess(product);
    }
    @GetMapping("/viewChangeItem")
    public CommonResponse<Object> viewChangeItem(@RequestParam("itemId") String itemId){
        Item item=catalogService.getItemByItemId(itemId);
        return CommonResponse.createForSuccess(item);
    }
    @GetMapping("/viewProductManage")
    public CommonResponse<Object> viewCategoryManage(){
        List<Category>categoryList=catalogService.getAllCategoryList();
        List<Product>productList=catalogService.getAllProduct();
        List<Item>itemList=catalogService.getAllItem();

        return CommonResponse.createForSuccess(categoryList,productList,itemList);
    }
    @GetMapping("/viewAddCategory")
    public CommonResponse<Object> viewAddCategory(){
        List<Category> categoryList=catalogService.getAllCategoryList();
        return CommonResponse.createForSuccess(categoryList);
    }
    @GetMapping("/viewAddProduct")
    public CommonResponse<Object> viewAddProduct(){
        List<Product> productlist=catalogService.getAllProduct();
        return CommonResponse.createForSuccess(productlist);
    }
    @GetMapping("/viewAddItem")
    public CommonResponse<Object> viewAddItem(){
        List<Item> itemlist=catalogService.getAllItem();
        return CommonResponse.createForSuccess(itemlist);
    }
    @GetMapping("/viewUserManage")
    public CommonResponse<Object> viewUserManage(){
         List<User> userList=userService.getAllUser();
        return CommonResponse.createForSuccess(userList);
    }
    @GetMapping("/viewChangeUser")
    public CommonResponse<Object> viewChangeUser(@RequestParam("userId") String userId){
        User user=userService.getUser(userId);
        return CommonResponse.createForSuccess(user);
    }
     @PostMapping("/saveUser")
    public CommonResponse<Object> saveUser(@RequestParam("userId") String userId,
                                           @RequestParam("password") String password,
                                           @RequestParam("firstname") String firstname,
                                           @RequestParam("lastname") String lastname,
                                           @RequestParam("phone") String phone,
                                           @RequestParam("email") String email){

        userService.saveUser(userId,password,firstname,lastname,phone,email);
        List<User> userList = userService.getAllUser();
        return CommonResponse.createForSuccess(userList);
    }
    @PostMapping("/removeOrder")
    public CommonResponse<Object> removeOrder(@RequestParam("orderId") int orderId){
        orderService.removeOrders(orderId);
        return CommonResponse.createForSuccess(orderService.getAllOrder());
    }
    @PostMapping("/searchOrder")
    public CommonResponse<Object> searchOrder(@RequestParam("keyword")String keyword){
        List<Order> orderList=orderService.searchOrderList(keyword);
        return CommonResponse.createForSuccess(orderList);
    }
    @PostMapping("/saveOrder")
    public CommonResponse<Object> saveOrder(@RequestParam("order") Order order,
                                            HttpSession session){
        System.out.println(order.getBillCity()+"saveOrder");
        boolean b=orderService.updateOrder(order);
        if(b){
            return CommonResponse.createForSuccess(orderService.getAllOrder());
        }
        else {
           String message = "申请失败，请重新输入！";
            session.setAttribute("message",message);
        return CommonResponse.createForSuccess(order);
        }
    }
    @GetMapping("/sendOrder")
    public CommonResponse<Object> sendOrder(@RequestParam("orderId") int orderId){
        orderService.UpdateSendOrderStatus(orderId);
        List<Order>orderList=orderService.getAllOrder();
        return CommonResponse.createForSuccess(orderList);
    }
    @GetMapping("/refundOrder")
    public CommonResponse<Object> refundOrder(@RequestParam("orderId") int orderId){
        orderService.CancleOrderStatus(orderId);
        List<Order>orderList=orderService.getAllOrder();
        return CommonResponse.createForSuccess(orderList);
    }

}
